package org.dromara.interceptor;

import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.BaseMultiTableInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.dromara.handler.PlusDataPermissionHandler;

import java.sql.Connection;

/**
 * @ClassName EY-PlusDataPermissionInterceptor
 * @Description org.dromara.interceptor
 * @Author menyj
 * @Date 2025/9/20
 **/
@Slf4j
public class PlusDataPermissionInterceptor extends BaseMultiTableInnerInterceptor implements InnerInterceptor {

    private final PlusDataPermissionHandler dataPermissionHandler;

    public PlusDataPermissionInterceptor(String mapperPackage) {
        this.dataPermissionHandler = new PlusDataPermissionHandler(mapperPackage);
    }

    /**
     * 构建表达式，用于处理表的数据权限
     * buildTableExpression 是  BaseMultiTableInnerInterceptor 的 抽象方法，它参与到了 builderExpression 和 andExpression
     * @param table
     * @param where
     * @param whereSegment
     * @return
     */
    @Override
    public Expression buildTableExpression(
            Table table, Expression where, String whereSegment) {
        System.out.println("buildTableExpression start");
        return null;
    }

    // TODO beforeQuery 在执行查询之前，检查并处理数据权限相关逻辑
    @Override
    public void beforeQuery(
            Executor executor, MappedStatement ms, Object parameter,
            RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        log.info("beforeQuery start");
        // 检查是否需要忽略数据权限处理
        if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) {
            return;
        }
        // 检查是否缺少有效的数据权限注解
//        if (dataPermissionHandler.invalid(ms.getId())) {
//            return;
//        }
        // 解析 sql 分配对应方法
        PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
        mpBs.sql(parserSingle(mpBs.sql(), ms.getId()));
    }

    // TDDO beforePrepare
    @Override
    public void beforePrepare(
            StatementHandler sh, Connection connection, Integer transactionTimeout) {
        log.info("beforePrepare start");
    }

    // TODO processSelect 处理 SELECT 查询语句中的 WHERE 条件
    @Override
    protected void processSelect(Select select, int index, String sql, Object obj) {
        log.info("processSelect start");
        System.out.println("sys processSelect start");
    }

    // TODO processUpdate 处理 UPDATE 语句中的 WHERE 条件

    // TODO processDelete 处理 DELETE 语句中的 WHERE 条件

    // TODO setWhere
    protected void setWhere(PlainSelect plainSelect, String mappedStatementId) {
        log.info("setWhere start");
    }
}
